<script setup>
import useAdminrouter from "@/router/index.js";
import {reactive, ref, onMounted} from 'vue'
import {
  editPasswordService
} from '@/api/netshop/customInfoMessage.js'
import {shopCustomStore} from '@/stores/modules/shop/index.js'
import {ElMessage} from "element-plus";

const store = shopCustomStore()
const router = useAdminrouter
let tData = reactive({
  form: {
    password: undefined,
    repassword: undefined,
  }
})
const ruleFormRef = ref()
const rules = reactive({
  password: [
    {
      required: true,
      message: '请输入新的密码',
      trigger: 'blur',
    },
    {pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur'}],
  repassword: [
    {
      required: true,
      message: '请再次输入上面的密码',
      trigger: 'blur',
    },
    {pattern: /^\S{6,15}$/, message: '密码必须是6-15位的非空字符', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        if (value !== tData.form.password) {
          callback('两次输入的密码必须相同')
        }
        callback() // 验证通过时调用回调函数，表示验证通过。
      }
    }
  ],
})
const save = (formEl) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      const id = store.custom_id
      await editPasswordService(tData.form, id)
          .then((res) => {
            ElMessage.warning('密码修改成功，即将退出到登陆界面')
            setTimeout(() => {
              store.CustomLogout()
              router.push({path: '/customlogin'})
            }, 2000)
          })
          .catch((err) => {
            console.log(err)
          })
    }
  })
}
</script>
<template>
  <el-card class="box-card" v-loading="loading">
    <template #header>
      <div class="card-header">
        <span>修改密码</span>
      </div>
    </template>
    <el-form
        ref="ruleFormRef"
        :model="tData.form"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
    >
      <el-form-item label="密码" prop="password">
        <el-input
            show-password
            v-model="tData.form.password"
            type="password"
            autocomplete="off"/>
      </el-form-item>
      <el-form-item label="再次确认" prop="repassword">
        <el-input
            v-model="tData.form.repassword"
            type="password"
            autocomplete="off"
            show-password/>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="footer">
        <el-button size="large" round type="primary" @click="save(ruleFormRef)">保存</el-button>
      </div>
    </template>
  </el-card>
</template>


<style scoped>
.box-card {
  width: 100%;
  height: 100%;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header span {
  color: #152844;
  font-weight: 600;
  font-size: 18px;
  line-height: 48px;
  height: 48px;
  margin-bottom: 4px;
  border-bottom: 1px solid #cedce4;
}

/*横线*/
.split-line {
  width: 100%;
  height: 2px;
  background: #dae6f9;
}

/*展示页*/
.list-content {
  text-align: center;
}

.list-content .el-image {
  margin: 0 auto;
}

.flex-view {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
}

.item {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  margin: 20px 0;
}

.label {
  width: 100px;
  color: #152844;
  font-weight: 600;
  font-size: 14px;
}

.right-box {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.avatar {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  margin-right: 16px;
}

.change-tips {
  -webkit-box-align: center;
  -ms-flex-align: center;
  align-items: center;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}

label {
  color: #4684e2;
  font-size: 14px;
  line-height: 22px;
  height: 22px;
  cursor: pointer;
  width: 90px;
  display: block;
}

.tip {
  color: #6f6f6f;
  font-size: 14px;
  height: 22px;
  line-height: 22px;
  margin: 0;
  width: 100%;
}

.right-box {
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
}

.input-dom {
  width: 400px;
}

.input-dom {
  background: #f8fafb;
  border-radius: 4px;
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  color: #152844;
  padding: 0 12px;
}

.tip {
  font-size: 12px;
  line-height: 16px;
  color: #6f6f6f;
  height: 16px;
  margin-top: 4px;
}

.intro {
  resize: none;
  background: #f8fafb;
  width: 100%;
  padding: 8px 12px;
  height: 82px;
  line-height: 22px;
  font-size: 14px;
  color: #152844;
}

.footer {
  float: right;
}
</style>